# encoding: utf-8


"""

@author: tongzhenguo

@time: 2020/4/8 上午10:51

@desc:

维度数据组合
- 字段通用抽取方法
- 标签体系映射
- 组合item特征、组合user特征、组合上下文特征
- 定义排序公式
- 列表去重
- 打散、加权、强插

"""
import json
import random

import pandas as pd


def common_field_extract(d, extract_fn):
    """字段通用抽取方法"""
    c_list = []
    if type(d) == str:
        d = json.loads(d)
    if not d:
        return []
    try:
        for dd in d:
            c = extract_fn(dd)
            c_list.append(c)
        return c_list
    except Exception as e:
        print("d:%s msg:%s", d, repr(e))
        return []


def mapping():
    """ 标签体系映射"""
    pass


def merge_feature(data, group_data, on, how):
    """组合item特征、组合user特征、组合上下文特征
    抽象各个特征为一个json,key为特征名(必须唯一)，val为对应特征值
    :param data:dataframe，输入数据
    :param group_data: dataframe, 维度数据
    :param on: 联表的连接列
    :param how: 联表方式，枚举值：inner、left、right、full
    """

    def fn(x1, x2):
        d1 = json.loads(x1)
        d2 = json.loads(x2)
        assert type(d1) == dict
        assert type(d2) == dict
        d = dict()
        d.update(d1)
        d.update(d2)
        return d

    data = pd.merge(data, group_data[[on, "feature"]], how=how, on=on)
    data["feature"] = data["feature_x"] + "|" + data["feature_y"]
    data["feature"] = data["feature"].apply(lambda x: fn(x.split("|")[0], x.split("|")[1]))
    del data["feature_x"]
    del data["feature_y"]
    return data


def rank(data, feature_col, **kwargs):
    """定义排序公式
    线性加权score=sum(Wi * Xi)

    :param data: dataframe
    :param feature_col: 特征字典
    :type kwargs: dict,权重系数
    """
    data["score"] = data[feature_col].apply(lambda x: sum(kwargs[k] * v for k, v in x.items()))
    return data


def rand_rerank(data):
    """pandas 实现随机打散"""
    return data.sample(frac=1.0)


def insert_list_by_step(insert_feeds, all_feeds, step=5):
    """强插固定位逻辑，支持step配置"""
    idx = step - 1
    while len(insert_feeds) and idx < len(all_feeds):
        if len(insert_feeds) > 0:
            rand_index = random.randint(0, len(insert_feeds) - 1)
            feed1 = insert_feeds[rand_index]
            if feed1 in all_feeds:
                all_feeds.remove(feed1)
            all_feeds.insert(idx, feed1)
            insert_feeds.remove(feed1)
            print("强插位置%s的feed_id : %s " % (idx + 1, feed1))
        idx += step
    return all_feeds


def hard_multi_rerank_in_window(data, source_rank_name, window_rank_name, window_size=20, **kwargs):
    """支持多列打散，分桶排序"""
    pass


def common_dedup(df, dedup_col):
    """
    统一去重
    :param df: dataframe,输入数据
    :param dedup_col: str,去重列
    :return: list
    """
    dedup_list = list()
    for ele in df[dedup_col].values.tolist():
        if ele not in dedup_list:
            dedup_list.append(ele)
    return dedup_list


def add_weight(df, col, is_reweight_fn, **kwargs):
    """提权"""
    df["is_rereweight"] = df[col].apply(is_reweight_fn)
    df = df[df.is_rereweight == True]
    other_data = df[df.has_halloween == False]
    df = pd.concat([df, other_data])
    return df


if __name__ == "__main__":
    pass
